<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import { listWaterQuality } from "@/api/iot/WaterQuality";
import { allListGroup } from '@/api/iot/group'
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './resize'

const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '240px'
        },
        chartData: {
            type: Array,
            // required: true
        },
        type: {
            type: Number | String,
            // default: 
        },
        isNumber: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            chart: null,
            isText: 'COD',
            yAxismax: 100,
            xData: [],
            yData: [],
            isType: 1,
            isData: [],
        }
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                // this.setOptions(val)
            }
        },
        type: {
            handler(val) {
                this.isType = val
                // this.setOptions(val)
                if (val === 1) {
                    this.isText = 'COD'
                    this.yAxismax = 100
                }
                else if (val === 2) {
                    this.isText = '氨氮'
                    this.yAxismax = 20
                }
                else if (val === 3) {
                    this.isText = '总氮'
                    this.yAxismax = 30
                }
                else if (val === 4) {
                    this.isText = '总磷'
                    this.yAxismax = 5
                }
                this.isEmptyList();
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.isEmptyList();
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            // this.setOptions(this.chartData)
            this.setOptions()
        },
        isEmptyList() {
            listWaterQuality({
                pageNum: 1,
                pageSize: 10,
                grouId: this.isNumber
            }).then(response => {
                if (response.code == 200) {
                    let arrayJson1 = [];
                    let arrayJson2 = [];
                    let arrayJson3 = [];
                    let arrayJson4 = [];
                    console.log(response.rows, 'responserowsrows ')
                    response.rows.forEach(item => {
                        if (item.cod) {
                            arrayJson1.push({
                                testTime: item.testTime,
                                vaule: item.cod,
                            })
                        }
                        if (item.ammoniaNitrogen) {
                            arrayJson2.push({
                                testTime: item.testTime,
                                vaule: item.ammoniaNitrogen,
                            })
                        }
                        if (item.totalNitrogen) {
                            arrayJson3.push({
                                testTime: item.testTime,
                                vaule: item.totalNitrogen,
                            })
                        }
                        if (item.totalPhosphorus) {
                            arrayJson4.push({
                                testTime: item.testTime,
                                vaule: item.totalPhosphorus,
                            })
                        }
                    })
                    if (this.isType == 1) {
                        this.isData = arrayJson1
                    }
                    else if (this.isType == 2) {
                        this.isData = arrayJson2
                    }
                    else if (this.isType == 3) {
                        this.isData = arrayJson3
                    }
                    else if (this.isType == 4) {
                        this.isData = arrayJson4
                    }
                    setTimeout(() => {
                        this.initChart();
                    }, 500)

                }
            })
        },
        setOptions() {
            console.log(this.isData, 'this.isData ')
            this.chart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        crossStyle: {
                            color: '#fff'
                        }
                    }
                },
                grid: {
                    top: '12%',
                    left: '3%',
                    right: '4%',
                    bottom: '12%',
                    containLabel: true
                },
                // legend: {
                //     bottom: 'bottom',
                //     textStyle: { //图例文字的样式
                //         color: '#fff',
                //         fontSize: 16
                //     },
                // },
                xAxis: [
                    {
                        type: 'category',
                        data: this.isData ? this.isData.map(item => item.testTime) : [],
                        axisPointer: {
                            type: 'shadow'
                        }
                    }
                ],
                yAxis: {
                    type: 'value',
                    min: 0,
                    max: this.yAxismax
                },

                series: [
                    {
                        name: this.isText,
                        type: 'line',
                        symbol: 'none',
                        sampling: 'lttb',
                        areaStyle: {},
                        data: this.isData ? this.isData.map(item => item.vaule) : [],
                        itemStyle: {
                            color: '#779DFF'
                        },
                    },
                ]

            })
        }
    }
}
</script>
